import { isUrl } from '../utils/utils';
import {getAdminInfo} from "../services/api";

const menuData = [
  {
    name: '资源管理',
    icon: 'table',
    path: 'resourceMgr',
    short: 'resource',
    children: [
      {
        name: '专利管理',
        path: 'patentList',
        short: 'resource:patent',
      },
      {
        name: '成果管理',
        path: 'achievementList',
        short: 'resource:achievements',
      },
      {
        name: '专家管理',
        path: 'expertList',
        short: 'resource:experts',
      },
      {
        name: '企业管理',
        path: 'enterpriseList',
        short: 'resource:enterprise',
      },
    ],
  },
  {
    name: '标签管理',
    icon: 'table',
    path: 'tagMgr',
    short: 'tag',
    children: [
      {
        name: '标签管理',
        path: 'tagList',
        short: 'tag:manage',
      },
      {
        name: '标签体系',
        path: 'tagSetup',
        short: 'tag:category',
      },
    ],
  },
  {
    name: '匹配管理',
    icon: 'table',
    path: 'matchMgr',
    short: 'match',
    children: [
      {
        name: '企业匹配',
        path: 'enterpriseMatch',
        short: 'match:enterprise',
      },
      {
        name: '需求匹配',
        path: 'requestMatch',
        short: 'match:request',
      },
    ],
  },
  {
    name: '系统管理',
    icon: 'table',
    path: 'systemMgr',
    short: 'system',
    children: [
      {
        name: '角色配置',
        path: 'role',
        short: 'system:role',
      },
      {
        name: '用户配置',
        path: 'admin',
        short: 'system:admin',
      },
    ],
  },
];

localStorage.setItem('menuData', JSON.stringify(menuData));

function dealPermissionMenu() {
  let current = localStorage.getItem('current');
  console.log('-------------current-----------------')
  console.log(current)
  if (current && current !== 'undefined') {
    return JSON.parse(current).menu;
  }
  return [];
}

function formatter(data, parentPath = '/', parentAuthority) {
  return data.map(item => {
    let { path } = item;
    if (!isUrl(path)) {
      path = parentPath + item.path;
    }
    const result = {
      ...item,
      path,
      authority: item.authority || parentAuthority,
    };
    if (item.children) {
      result.children = formatter(item.children, `${parentPath}${item.path}/`, item.authority);
    }
    return result;
  });
}

export const getMenuData = () => formatter(dealPermissionMenu());
